package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.AttentionDao;
import com.gdit.zax.dao.CollectDao;
import com.gdit.zax.dao.CommentDao;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.MedalDao;
import com.gdit.zax.dao.SignDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.MD5;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;
import com.gdit.zax.utils.webUtil;

public class GetUserInfo extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int uid = TextUtil.String2int(request.getParameter("uid"), -1);
		String accesstoken = request.getParameter("accesstoken");
		int myid = VeriFicUtil.getVFID(accesstoken);
		
		//检测参数
		if (uid<0) {
			O.printFail(response, 1, "请检查uid参数!");
			return;
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		UserInfo uinfo = udao.getInfo(uid);
		AttentionDao adao = dao.getAttentionDao();
		DynamicDao ddao = dao.getDynamicDao();
		CollectDao cdao = dao.getCollectDao();
		MedalDao mdao = dao.getMedalDao();
		SignDao sdao = dao.getSignDao();
		CommentDao cmdao = dao.getCommentDao();
		
		if (uinfo==null) {
			O.printFail(response, 1, "该用户还没有注册!");
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", uinfo.getId());
		
		map.put("nick", uinfo.getNick());
		map.put("username", uinfo.getUsername());
		map.put("pic", webUtil.getWebAbsoluteUrl(uinfo.getUserhead(), Constant.PIC_userhead));
		map.put("content", uinfo.getIntroduction());
		map.put("sex", uinfo.getSex());
		map.put("fanssum", adao.getFransSum(uid));
		map.put("affsum", adao.getAttentionSum(uid));
		map.put("dynamicsum", ddao.getMyAllDynamicsum(uid));
		map.put("collectcourse", cdao.getCourseCollectSum(uid));
		map.put("donecourse", udao.getDoneCourseSum(uid));
		map.put("medalsum", mdao.getUserMedalCount(uid));
		map.put("signincount", sdao.getSignSum(uid));
		map.put("sendcommentsum", cmdao.getUseSendCommentSum(uid));
		map.put("commentsum", cmdao.getUseCommentSum(uid));
		map.put("age", uinfo.getAge());
		map.put("regtime", uinfo.getAddtime());
		map.put("logintime", uinfo.getLogintime());
		map.put("truename", uinfo.getTruename());
		map.put("email", uinfo.getEmail());
		map.put("score", uinfo.getScore());
		map.put("phone", uinfo.getPhone());
		map.put("introduction", uinfo.getIntroduction());
		if (myid==Integer.MAX_VALUE || myid == Integer.MIN_VALUE) {
			map.put("isaffent", Constant.AFFTENSTAT_NOLOGIN);
		} else if (adao.isAttention(myid, uid)) {
			map.put("isaffent", Constant.AFFTENSTAT_YES);
		} else {
			map.put("isaffent", Constant.AFFTENSTAT_NO);
		}
		
		O.printSuccess(response, map);
		
	}

}
